package br.uff.gdse.positionevaluation.evaluators;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import br.uff.gdse.positionevaluation.entity.Result;
import br.uff.gdse.positionevaluation.queries.XMLQuery;

public class XQueryProcessorEvaluator {

	private static final int COUNT_EVALUATED_QUERIES = 20;
	
	public List<Result> evaluate(String command, String fileNameQueryPrefix, String fileNameQuerySufix){
		
		//int[] positionValues = getPositionValues(query);
		List<Result> results = new ArrayList<Result>();
		
		for (int i = 1; i <= COUNT_EVALUATED_QUERIES; i++) {
			String queryFile = fileNameQueryPrefix + i + fileNameQuerySufix;
			String completeCommand = command + queryFile;
			//String positionQuery = query.getQuery()+"[position()=" + positionValues[i] + "]";
//			XMLQuery xmlPositionQuery = new XMLQuery(query.getCollection(), positionQuery);
			
			double[] responseTimes = new double[10];
			for (int j = 0; j < responseTimes.length; j++) {
				long initialTime = System.currentTimeMillis();
				try {
					Process p = Runtime.getRuntime().exec(completeCommand);
					p.waitFor();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				long elapsedTime = System.currentTimeMillis() - initialTime;
				System.out.println(elapsedTime);
				responseTimes[j] = elapsedTime;
			}
			XMLQuery query = readQueryFromFile(queryFile);
			Result result = new Result(responseTimes, query);
			results.add(result);
		}
		
		return results;
		
	}

	private XMLQuery readQueryFromFile(String queryFile) {
		String strLine = "";
		try {
			FileInputStream fstream = new FileInputStream(queryFile);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			strLine = br.readLine();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		XMLQuery q = new XMLQuery("", strLine);

		return q;
	}

//	private int[] getPositionValues(XMLQuery query) {
//		String countQuery = "count(" + query.getQuery() + ")";
//		XMLQuery xmlCountQuery = new XMLQuery(query.getCollection(), countQuery);
//		
//		List<Object> result = dbInterface.executeQuery(xmlCountQuery);
//		int[] positions;
//		int count = Integer.parseInt(result.get(0).toString());
//		if(count>COUNT_EVALUATED_QUERIES){
//			positions = new int[COUNT_EVALUATED_QUERIES];
//			int intervalo = count / COUNT_EVALUATED_QUERIES;
//			for (int i = 0; i < positions.length; i++) {
//				positions[i] = intervalo*i+1;
//			}
//		}else{
//			positions = new int[count];
//			for (int i = 0; i < positions.length; i++) {
//				positions[i] = i+1;
//			}
//		}
//		return positions;
//	}
}
